package com.yanqu.road.server.cmd.activity.xingyoushanshui.cross;

import com.yanqu.road.entity.activity.youxing.XingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.config.XingYouConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shanshui.XingYouShanShuiModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.shanshui.XingYouShanShuiMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;

@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_DICE_FROM_CROSS,desc = "投骰子(跨服返回)")
public class ShanShuiDiceFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        XingYouShanShuiProto.CrossShanShuiDiceRsp rsp = XingYouShanShuiProto.CrossShanShuiDiceRsp.parseFrom(packet.getBytes());

        XingYouShanShuiProto.ShanShuiDiceRsp clientMsg = rsp.getClientMsg();

        XingYouConfig config = XingYouShanShuiMgr.getConfig();

        String cost = null;
        if(rsp.getDiceType() == 2){
            cost = PropertyHelper.parseGoodsToString(config.getSHANSHUI_EXCLUSIVE_DICE_ID(), BigInteger.ONE);
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
        }else if(rsp.getDiceType() == 3){
            cost = PropertyHelper.parseGoodsToString(config.getSHANSHUI_MECHANICAL_DICE_ID(), BigInteger.ONE);
            player.getModule(PreReduceModule.class).restorePreReduce(cost);
        }

        if(clientMsg.getRet() == 0){

            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            if(cost != null){
                currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(cost), eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiDicConsume);
            }

            //消耗通行证
            if(rsp.hasUsePassIdNum()){
                int usePassIdNum = rsp.getUsePassIdNum();
                currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(config.getSHANSHUI_PASS_ID() + "=" + usePassIdNum), eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiDicConsume);
            }

            if(!StringUtils.isNullOrEmpty(rsp.getReward())){
                Property reward = PropertyHelper.parseStringToProperty(rsp.getReward());
                //银票不入背包
                reward.removeProperty(config.getSHANSHUI_SILVER_COIN_ID());
                currencyModule.addCurrency(reward, eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiDicReward);
            }

            if(rsp.hasTouchMall() && rsp.getTouchMall()){
                //player.getModule(UserActivityMallTouchModule.class).addTriggerMall(config.getActivityInfo().getActivityId());
            }
        }

        player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_DICE, clientMsg.toBuilder());

        XingYouShanShuiUserData userData = player.getModule(XingYouShanShuiModule.class).getUserData();
        if(rsp.hasRound()) {
            int round = rsp.getRound();
            if (userData != null) {
                userData.setRound(round);
                player.notifyListener(eGamePlayerEventType.XingYouShanShuiUserRoundCount.getValue(), round);
            }
        }

        if(rsp.hasUseEnergyTotal()) {
            if (userData != null) {
                userData.setTotalEnergy(rsp.getUseEnergyTotal());
//                player.notifyListener(eGamePlayerEventType.XingYouShanShuiUserDiceCount.getValue(), userData.getTotalEnergy());
            }
        }
        if(rsp.hasSilverCoinTotal()){
            if (userData != null) {
                userData.setTotalSilverCoin(rsp.getSilverCoinTotal());
                player.notifyListener(eGamePlayerEventType.XingYouShanShuiUserSliverRank.getValue(), userData.getTotalSilverCoin());
            }
        }

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
